<?php
  //Loader::loadFile('config.php', 'modules/VoteThailand');
  /**
  * initialise module
  *
  */
  function VoteThailandPoll_init() {
      mysql_query("SET NAME UTF8");
    //Create votethailandpoll_province table
    if (!DBUtil::createTable('votethailandpoll_province')) {
        return false;
    }
    //Create votethailandpoll_district table
    if (!DBUtil::createTable('votethailandpoll_district')) {
        return false;
    }
    //Create votethailandpoll_province table
    if (!DBUtil::createTable('votethailandpoll_tambon')) {
        return false;
    }
    //Create votethailandpoll_village table
    if (!DBUtil::createTable('votethailandpoll_village')) {
        return false;
    }

    //Create votethailandpollcenter_person_canvasser table
    if (!DBUtil::createTable('votethailandpoll_house_no')) {
        return false;
    }

    //Create votethailandpoll_party table
    if (!DBUtil::createTable('votethailandpoll_party')) {
        return false;
    }

    //Create votethailandpoll_villagepoll table
    if (!DBUtil::createTable('votethailandpoll_villagepoll')) {
        return false;
    }
    
        //Create votedatacenter_gender table
    if (!DBUtil::createTable('votethailandpoll_gender')) {
        return false;
    }

    //Create votedatacenter_person table
    if (!DBUtil::createTable('votethailandpoll_person')) {
        return false;
    }

    VoteThailandPoll_init_data();
    VoteThailandPoll_init_folder();
    return true;
  }


  function VoteThailandPoll_init_folder(){

    if (!is_dir("pnTemp/votethailandpoll_upload/")) {
      mkdir("pnTemp/votethailandpoll_upload/", 0755);
    }
    
  }

  function VoteThailandPoll_delete() {

    DBUtil::dropTable('votethailandpoll_province');
    DBUtil::dropTable('votethailandpoll_district');
    DBUtil::dropTable('votethailandpoll_tambon');
    DBUtil::dropTable('votethailandpoll_village');
    DBUtil::dropTable('votethailandpoll_house_no');
    DBUtil::dropTable('votethailandpoll_party');
    DBUtil::dropTable('votethailandpoll_villagepoll');
    DBUtil::dropTable('votethailandpoll_gender');
    DBUtil::dropTable('votethailandpoll_person');

/*
    DROP TABLE `zk_votethailandpoll_province`;
    DROP TABLE `zk_votethailandpoll_district`;
    DROP TABLE `zk_votethailandpoll_tambon`;
    DROP TABLE `zk_votethailandpoll_village`;
    DROP TABLE `zk_votethailandpoll_party`;
    DROP TABLE `zk_votethailandpoll_poll`;
*/
    return true;
  }

   function VoteThailandPoll_init_data(){
    setlocale ( LC_ALL, 'en_US.UTF-8' );
    //Init province data
    $file = "modules/VoteThailandPoll/data/province.csv";
    $handle = fopen($file, "r");

    if($handle!==false){
      $objArray =array();
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
          $provinceArray[] = array(
                                    'id'        => $data[0],
                                    'name'      => $data[1],
                                    'latitude'  => $data[2],
                                    'longitude' => $data[3]
                                  );
      }

      //print_r($provinceArray); exit;

      DBUtil::insertObjectArray($provinceArray, 'votethailandpoll_province', true);
      unset($provinceArray);
      unset($data);
      fclose($handle);
    }

/*
      $provinceArray[] = array(
                                'id'        => 1,
                                'name'      => 'แซมอ่อน',
                                'latitude'  => 18.00,
                                'longitude' => 103.00
                              );
      DBUtil::insertObjectArray($provinceArray, 'votethailandpoll_province', true);
*/
    //Init district data
    $file = "modules/VoteThailandPoll/data/district.csv";
    $handle = fopen($file, "r");
    if($handle!==false){
      $districtArray =array();
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $districtArray[] = array(
                                      'id'        => $data[0],
                                      'pro_id'    => $data[1],
                                      'name'      => $data[2],
                                      'latitude'  => $data[3],
                                      'longitude' => $data[4]
                                    );
      }
      //DBUtilEx::setCharacterSet('utf8');
      DBUtil::insertObjectArray($districtArray, 'votethailandpoll_district', true);
      unset($districtArray);
      unset($data);
      fclose($handle);
    }


    //Init tambon data
    $file = "modules/VoteThailandPoll/data/tambon.csv";
    $handle = fopen($file, "r");
    if($handle!==false){
      $tambonArray =array();
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $tambonArray[] = array(
                                      'id'        => $data[0],
                                      'pro_id'    => $data[1],
                                      'dis_id'    => $data[2],
                                      'name'      => $data[3],
                                      'latitude'  => $data[4],
                                      'longitude' => $data[5]
                                    );
      }
      DBUtil::insertObjectArray($tambonArray, 'votethailandpoll_tambon', true);
      unset($tambonArray);
      unset($data);
      fclose($handle);
    }

    //Init village data
    $file = "modules/VoteThailandPoll/data/village.csv";
    $handle = fopen($file, "r");
    if($handle!==false){
      $villageArray =array();
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $villageArray[] = array(
                                      'id'        => $data[0],
                                      'pro_id'    => $data[1],
                                      'dis_id'    => $data[2],
                                      'tam_id'    => $data[3],
                                      'moo'       => $data[4],
                                      'name'      => $data[5],
                                      'latitude'  => $data[6],
                                      'longitude' => $data[7]
                                    );
      }
      DBUtil::insertObjectArray($villageArray, 'votethailandpoll_village', true);
      unset($villageArray);
      unset($data);
      fclose($handle);
    }

  }

  function VoteThailandPoll_fixEncoding($in_str) {
    $cur_encoding = mb_detect_encoding($in_str) ;

    if($cur_encoding == "UTF-8" && mb_check_encoding($in_str,"UTF-8"))
      return $in_str;
    else
      return utf8_encode($in_str);
  } 

